package com.victor.pomelo.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.victor.pomelo.dao.MenuMapper;
import com.victor.pomelo.model.Menu;
import com.victor.pomelo.model.Resource;
import com.victor.pomelo.service.MenuService;

@Service
public class MenuServiceImpl implements MenuService {
	@Autowired
	private MenuMapper menuMapper;

	@Override
	public List<Menu> findMainMenus(Map<Long, Resource> resources) {
		// 1、查询系统设置的所有菜单列表
		List<Menu> menus = menuMapper.findSubMenus(new Long(0)); // 获取根目录菜单列表
		for (Menu menu : menus) {
			menu.setSubMenus(menuMapper.findSubMenus(menu.getMenuid())); // 形成菜单两级列表
		}

		// 2、循环检查是否有权操作，如果有权操作则标记为显示，父级菜单显示
		for (Menu menu : menus) {
			for (Menu submenu : menu.getSubMenus()) {
				if (submenu.getResourceid() != null
						&& resources.get(submenu.getResourceid()) != null) {
					submenu.setVisible(true);
					menu.setVisible(true);
				}
			} 
		}
		return menus;
	}

}
